package top.zhangjianyong.tools.config.mybatis;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * mybatis-plus 配置
 *
 * @author zhang
 * @date 16:26 2019/5/20 0020
 **/
@Configuration
@MapperScan("top.zhangjianyong.tools.mapper")
public class MybatisPlusConfig {

    /***
     * plus 的性能优化
     */
   /* @Bean
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        //<!-- SQL 执行性能分析，开发环境使用，线上不推荐。 maxTime 指的是 sql 最大执行时长 -->
        //performanceInterceptor.setMaxTime(1000);
        //<!--SQL是否格式化 默认false-->
        performanceInterceptor.setFormat(false);
        return performanceInterceptor;
    }*/
    @Bean
    public IdentifierGenerator idGenerator(@Value("${id-worker.worker-id}") long workerId,
                                           @Value("${id-worker.data-center-id}") long dataCenterId,
                                           @Value("${id-worker.sequence}") long sequence) {
        return new CustomIdGenerator(workerId, dataCenterId, sequence);
    }

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); //分页插件
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); //乐观锁插件
        return interceptor;
    }

}
